package com.papper.common.service.base;

import com.papper.common.bean.base.BaseConstMcc;
import com.papper.common.cache.CacheConstants;
import com.papper.common.cache.redis.CacheClosureNullDefault;
import com.papper.common.mapper.base.BaseConstMccMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * The type Base const mcc service.
 *
 * @Author:LaJiao
 * @Date:2022-03-29 10 :06:49
 * @Description:商户类别码
 */
@Service("baseConstMccService")
public class BaseConstMccService extends ServiceImpl<BaseConstMccMapper, BaseConstMcc> {

    @Override
    protected void clearCaches(BaseConstMcc entity) {
        super.clearCaches(entity);
        cache.delete(CacheConstants.Keys(getModel(), entity.getMccCode()));
    }

    /**
     * Find by code base const mcc.
     *
     * @param mccCode the mcc code
     * @return the base const mcc
     */
    public BaseConstMcc findByCode(final String mccCode) {
        if (StringTool.isEmpty(mccCode)) {
            return null;
        }
        return cache.getAndSet(new CacheClosureNullDefault<BaseConstMcc>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), mccCode);
            }

            @Override
            public BaseConstMcc getValue() {
                Example          example  = new Example(BaseConstMcc.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("mccCode", mccCode);
                return findOne(example);
            }

            @Override
            public Class getTypeReference() {
                return BaseConstMcc.class;
            }
        });
    }
}
